import axios from 'axios'
import router from '../router'

//mint UI 框架，若不需要请全部注释掉
// import {Indicator, Toast} from 'mint-ui'
import { Toast } from 'vant'


export const baseURL = process.env.APP_API_BASE_URL

console.log(baseURL)

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
// axios.defaults.headers.post['Content-Type'] = 'application/json';

axios.defaults.baseURL = baseURL || '/'

// 添加请求拦截器
axios.interceptors.request.use(config => {
    // console.log(config);
    // Toast.loading({
    //     message: '加载中...',
    //     forbidClick: true,
    //     loadingType: 'spinner',
    //   });
// Toast(config);
    return config
  },
  err => {
//   Toast.close()
    // console.log(error); //调试用
    Toast({
      message: '加载超时',
      position: 'middle',
      duration: 3000
    })
    return Promise.reject(err)
  })


//返回一个Promise
/**
 *
 * @param url
 * @param params
 * @param type
 * @param config
 * @deprecated 过时功能封装完善 请使用 request.js
 */
export function fetch(url, params, type, config) {
  return new Promise((resolve, reject) => {
    if (type === 'post') {
      axios.post(url, params, config)
        .then(response => {
          if (response.data.status === '3210') {
            localStorage.setItem('mytoken', '')
          }
          resolve(response.data)
        }, err => {
          console.log(err)
          reject(err)
        })
        .catch((error) => {
          reject(error)
        })
    }
    if (type === 'put') {
      axios.put(url, params)
        .then(response => {
          if (response.data.status === '3210') {
            localStorage.setItem('mytoken', '')
          }
          resolve(response.data)
        }, err => {
          console.log(err)
          reject(err)
        })
        .catch((error) => {
          reject(error)
        })
    } else if (type === 'get') {
      axios.get(url, { params })
        .then(response => {
          if (response.data.status === '3210') {
            localStorage.setItem('mytoken', '')
          }
          resolve(response.data)
        }, err => {
          reject(err)
        })
        .catch((error) => {
          reject(error)
        })
    } else if (type === 'patch') {
      axios.patch(url, params)
        .then(response => {
          resolve(response.data)
        }, err => {
          reject(err)
        })
        .catch((error) => {
          reject(error)
        })
    } else if (type === 'delete') {
      axios.delete(url + '/' + params)
        .then(response => {
          resolve(response.data)
        }, err => {
          reject(err)
        })
        .catch((error) => {
          reject(error)
        })
    }
    // 成员详情
    else if (type === 'detail') {
      axios.get(url + '/' + params)
        .then(response => {
          resolve(response.data)
        }, err => {
          reject(err)
        })
        .catch((error) => {
          reject(error)
        })
    }

    // 解除关系
    else if (type === 'deleteA') {
      axios.delete(url)
        .then(response => {
          resolve(response.data)
        }, err => {
          reject(err)
        })
        .catch((error) => {
          reject(error)
        })
    }

  })
}


router.beforeEach((to, from, next) => {
  // 如果已经登录，那我不干涉你，让你随便访问
  let token = localStorage.getItem('mytoken')
  if (token) {
    next()
  } else {
    if (to.path !== '/login' && to.path !== '/Passwordlogin') {
      console.log('哈哈被我拦截了')
      // 如果没有登录，但你访问其他需要登录的页面，那我就让你跳到登录页面去
      next({ path: '/login' })
    } else {
      next()
    }
  }
})
